let express = require('express');
let path = require('path');
let config = require('./config/config');
let {page} = require('./public/entry.server.js');
let axios = require('axios');
let app = express();
let port = 8003;

app.use(require('compression')());
require('asset-require-hook')({
    name: '/[hash].[ext]',
    publicPath: '/',
    extensions: ['jpg', 'png', 'gif', 'webp', 'svg'],
    limit: 2048,
});

axios.defaults.baseURL = config.href;

app.use(express.static(path.join(__dirname, './public')));

app.all('*', function(req, res) {
    axios.interceptors.response.use(function(response) {
        return response;
    }, function(error) {
        if (error.response) {
            switch (error.response.status) {
                case 500:
                    res.redirect('/error', {
                        statusCode: 500,
                    });
                    break;
                case 503:
                    res.redirect('/error', {
                        statusCode: 503,
                    });
                    break;
                case 502:
                    res.redirect('/error', {
                        statusCode: 502,
                    });
                    break;
                default:
                    res.redirect('/error', {
                        statusCode: 404,
                    });
                    break;
            }
        }
        return Promise.reject(error);
    });
    let props = {
        baseUrl: config.href,
    };
    axios.all([
        axios.get('/columns/index/pictures', {
            params: {
                'column_name': 'index',
                'position': '1',
            },
        })]).then(function(response) {
        props.data = response[0].data;
        let html = page(props, req);
        res.end(html);
    }).catch(function(error) {

    });
});

app.get('/', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    axios.all([
        axios.get('/columns/index/pictures', {
            params: {
                'column_name': 'index',
                'position': '1',
            },
        })]).then(function(response) {
        props.data = response[0].data;
        let html = page(props, req);
        res.end(html);
    }).catch(function(error) {

    });
});

app.get('/introduction', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/introduction/:id', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/orders/:id', function(req, res) {
    let html = page({
        baseUrl: config.href,
    }, req);
    res.end(html);
});


app.get('/personal', function(req, res) {
    let html = page({
        baseUrl: config.href,
    }, req);
    res.end(html);
});

app.get('/motel/:id', function(req, res) {
    let html = page({
        baseUrl: config.href,
    }, req);
    res.end(html);
});


app.get('/login', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});
app.get('/resgister', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/forgot', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/about', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/product', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/join', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/contact', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});


app.get('/manage', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/condition', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/recruit', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/present', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.get('/error', function(req, res) {
    let props = {
        baseUrl: config.href,
    };
    let html = page(props, req);
    res.end(html);
});

app.listen(port, function() {

});
